﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CSharpDevTools.Core;
using HandyControl.Controls;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDevTools.ViewModels
{
    internal class MD5ControlViewModel : EncryptBaseViewModel
    {
        public RelayCommand EncryptFileCmd => new(EncryptFile);

        protected override void Encrypt()
        {
            if (string.IsNullOrEmpty(this.OriginalData))
            {
                MessageBox.Error("原始数据不能为空！", "错误");
                return;
            }

            this.EncryptedData = MD5Helper.Encrypt(this.OriginalData);
        }

        private void EncryptFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog().Value)
            {
                try
                {
                    this.OriginalData = string.Empty;
                    FileInfo fi = new FileInfo(openFileDialog.FileName);
                    this.EncryptedData = MD5Helper.Encrypt(fi);
                }
                catch (Exception ex)
                {
                    MessageBox.Error(ex.Message, "错误");
                }
            }
        }
    }
}
